﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Xml.Linq;
using Infragistics.Controls.Grids;
using StartupSolution.ViewModel;


namespace StartupSolution.Views.Localization
{
    public partial class GridResourceStrings : Page
    {
        public GridResourceStrings()
        {
            // TODO: Apply the resources to the xamGrid control.
            // If you want to implement this functionality in the code-behind, uncomment the following line
            //XamGrid.RegisterResources("StartupSolution.Resources.ResourceStrings", typeof(GridResourceStrings).Assembly);
           
            InitializeComponent();
            Loaded += new RoutedEventHandler(ResourceStrings_Loaded);
        }

        /*The following method unregisters the resource string when the page is navigated away from*/
        /*protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
            XamGrid.UnregisterResources("StartupSolution.Resources.ResourceStrings");
            base.OnNavigatingFrom(e);
        }*/

        void ResourceStrings_Loaded(object sender, RoutedEventArgs e)
        {
            LoadXMLData();
        }

        private void LoadXMLData()
        {
            // 	Loads the XML file and creates a XDocument object
            XDocument doc = XDocument.Load("Datasource/Products.xml");

            // Run LINQ query to retrieve the necessary data
            var data = (from d in doc.Descendants("Products")
                        select new Product
                        {
                            // Gets ProductID field
                            SKU = d.Element("ProductID").Value,
                            // Gets ProductName field
                            Name = d.Element("ProductName").Value,
                            // Gets Category field 
                            Category = d.Element("Category").Value,
                            // Gets Supplier field 
                            Supplier = d.Element("Supplier").Value,
                            // Gets QuantityPerUnit field
                            QuantityPerUnit = d.Element("QuantityPerUnit").Value
                            // TODO: Retrieve all the fields from XDocument and fill the XamGrid ItemSource
                        }).ToList<Product>();

            // Set the ItemsSource to generate the columns.
            igGrid.ItemsSource = data;
        }
    }
}
